package com.coder4j.system.controller;

import com.coder4j.system.entity.BizRefEntity;
import com.coder4j.system.service.BizRefService;
import com.coder4j.system.vo.BizRefQuery;
import com.coder4j.system.vo.BizRefVO;
import com.coder4j.system.vo.BizRefEO;
import com.coder4j.starter.web.api.ApiNote;
import com.coder4j.starter.mybatis.BasePage;
import com.coder4j.starter.tools.vo.R;
import com.coder4j.starter.web.util.EasyExcelUtils;
import com.coder4j.starter.tools.util.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 关系表
 * @author keller
 * @date 2024-12-05 10:46:31
 */
@Slf4j
@RestController
@RequestMapping("/bizRef")
public class BizRefController {
    @Resource
    private BizRefService service;

    @Resource
    private HttpServletResponse response;

    /**
     * 按ID查询关系表
     * @param id BizRef.id
     * @return 关系表对象
     */
    @GetMapping("/getById")
    @ApiNote(name="按ID查询关系表",createName = "keller",createTime = "2024-12-05 10:46:31")
    public R<BizRefVO> getById( Long id){

        return R.success(service.baseGetVOById(id));
    }

    /**
     * 保存关系表
     * 有 id 字段，更新；否则新增
     * @param bizRef BizRef对象
     * @return BizRef对象
     */
    @PostMapping("/save")
    @ApiNote(name="保存关系表",createName = "keller",createTime = "2024-12-05 10:46:31")
    public R save(@RequestBody BizRefVO bizRef){

        if(ObjectUtils.hasEmpty(bizRef.getType(),bizRef.getSourceId(),bizRef.getTargetId())){
            return R.paramError();
        }

        service.baseSaveVO(bizRef);
        return R.success();
    }

    /**
     * 查询列表
     * @param query 查询条件
     * @return 数据列表
     */
    @PostMapping("/page")
    @ApiNote(name="查询关系表列表",createName = "keller",createTime = "2024-12-05 10:46:31")
    public R<BasePage<BizRefVO>> getList(@RequestBody BizRefQuery query){
        if(query == null){
            query = new BizRefQuery();
        }
        return R.success(service.baseQueryVOPage(query));
    }

    /**
     * 导出列表
     * @param query 查询条件
     */
    @PostMapping("/export")
    @ApiNote(name="导出关系表列表",createName = "keller",createTime = "2024-12-05 10:46:31")
    public void export(@RequestBody BizRefQuery query){
        if(query == null){
            query = new BizRefQuery();
        }
        List<BizRefEntity> list = service.baseQueryList(query);
                List<BizRefEO> eoList = new ArrayList<>();
                for (BizRefEntity entity : list) {
                    BizRefEO eo = new BizRefEO();
                    BeanUtils.copyProperties(entity,eo);
                    eoList.add(eo);
                }
                EasyExcelUtils.exportClass(response,"BizRefList",eoList);
    }


    /**
     * 删除关系表
     * 如果Entity继承了 BaseSoftDeleteEntity，使用逻辑删除；否则，使用物理删除
     * @param id BizRef.id
     * @return 删除的数据
     */
    @PostMapping("/deleteById")
    @ApiNote(name="删除关系表",createName = "keller",createTime = "2024-12-05 10:46:31")
    public R deleteById( Long id){
        service.baseDeleteById(id);
        return R.success();
    }
}
